package com.ihr360.authority.sharing.privilege;

import com.ihr360.authority.sharing.service.AuthUserResourceService;
import com.ihr360.commons.data.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class GetAllPrivilege<ID> extends AbstractBizClassPrivilege<ID> {
    final Logger logger = LoggerFactory.getLogger(getClass());
    public GetAllPrivilege(AuthUserResourceService authUserResourceService, IAccessControlClass iAccessControlClass) {
        super(iAccessControlClass);
        this.authUserResourceService = authUserResourceService;
        this.method = AccessControlMethod.METHODS.getAll;
    }

    @Override
    public Map<ID, Integer> getUserResourcesPrivilege() {
        return userResourcesPrivilege;
    }

    @Override
    public Map<ID, Integer> getUserResourcesPublicity() {
        return userResourcesPublicity;
    }

    public boolean isAccessControl() {
        return accessControl;
    }

    public void validate() {
    }

    public GetAllPrivilege doValidate() {
        boolean noAccessControl = noAccessControl(this.method);
        if (!noAccessControl) {
            Pair<Map<Object, Integer>, Map<Object, Integer>> pair = authUserResourceService
                    .queryUserResourcesPrivilege(iAccessControlClass.getEntityClass(), iAccessControlClass.getIdClass());
            if (pair != null) {
                toIDKeyMap(pair.getFirst(), pair.getSecond());
            }

            validate();
        }
        this.accessControl = !noAccessControl;
        return this;
    }
}